﻿//定义js命名空间
Ext.namespace("FsCarInfo");
Ext.namespace("FsCarInfo.Car");

//Car FieldArray
FsCarInfo.Car.BaseFieldsArray = new Array(
    { name: 'Id', type: 'string' },
    { name: 'TDeport', type: 'string' },
    { name: 'Brand', type: 'string' },
    { name: 'Model', type: 'string' },
    { name: 'Color', type: 'string' },
    { name: 'Engin_ID', type: 'string' },
    { name: 'Frame_ID', type: 'string' },
    { name: 'Prod_Date', type: 'date' },
    { name: 'Miles', type: 'int' },
    { name: 'Appraisal', type: 'float' },
    { name: 'Cost', type: 'float' },
    { name: 'VatID', type: 'string' },
    { name: 'Remark', type: 'string' },
    { name: 'Atm_GJB', type: 'int' },
    { name: 'Atm_BT', type: 'int' },
    { name: 'Atm_SMS', type: 'int' },
    { name: 'Atm_DYQ', type: 'int' },
    { name: 'Atm_BXK', type: 'int' },
    { name: 'Atm_KEY', type: 'int' },
    { name: 'Atm_MAST', type: 'int' },
    { name: 'Atm_WIPER', type: 'int' },
    { name: 'Atm_CERTIFICATE', type: 'int' },
    { name: 'Remark', type: 'string' }
);

//Car JsonReader
FsCarInfo.Car.JsonReader = Ext.extend(Infrastructure.BaseJsonReader, {
    constructor: function (cfg) {
        cfg = cfg || {};
        FsCarInfo.Car.JsonReader.superclass.constructor.call(this, Ext.apply({
            fields: FsCarInfo.Car.BaseFieldsArray
        }, cfg));
    }
});

//Car JsonStore
FsCarInfo.Car.JsonStore = Ext.extend(Infrastructure.BaseJsonStore, {
    constructor: function (cfg) {
        cfg = cfg || {};
        FsCarInfo.Car.JsonStore.superclass.constructor.call(this, Ext.apply({
            url: "/Car/GetPlist",
            remoteSort: true,
            reader: new FsCarInfo.Car.JsonReader(),
            listeners: {
                'remove': function (thiz, record, index) {
                    Ext.Ajax.request({
                        url: '/Car/Delete?Id=' + record.data.Id,
                        success: function (response, opts) {
                            var result = Ext.util.JSON.decode(response.responseText);
                            if (result.success == false) {
                                JsHelper.ShowError(result.msg);
                                this.insert(0, record);
                            }
                        }
                    });
                }
            }
        }, cfg));
    }
});

//Car Default GridToolbar
FsCarInfo.Car.DefaultToolbar = Ext.extend(Ext.Toolbar, {
    tbargrid: null,
    constructor: function (cfg) {
        cfg = cfg || {};
        FsCarInfo.Car.DefaultToolbar.superclass.constructor.call(this, Ext.apply({
            items: [{
                xtype: "tbaraddbtn",
                handler: function (btn, e) {
                    var carEditForm = new FsCarInfo.Car.EditForm({
                        buttons: [new Ext.ux.SubmitBtn({
                            handler: function () {
                                if (carEditForm.getForm().isValid()) {
                                    carEditForm.getForm().submit({
                                        url: '/Car/SaveOrUpdate',
                                        params: {
                                            'Id': (updateEdit) ? id : ""
                                        },
                                        success: function (form, action) {
                                            cfg.tbargrid.getStore().setDefaultSort('CreateTime', 'DESC');
                                            cfg.tbargrid.load({// 刷新表格
                                                params: {
                                                    start: 0,
                                                    limit: ALL_PAGESIZE_SETTING
                                                }
                                            });
                                            carEditWin.destroy(); // 关闭窗口
                                        },
                                        failure: function (form, action) {
                                            JsHelper.ShowError(action.result.msg);
                                        }
                                    });
                                }
                            }
                        }),
                        new Ext.ux.CancelBtn({
                            handler: function () {
                                alert(this.ownerCt);
                                carEditWin.destroy();
                            }
                        })]
                    });
                    var carEditWin = new FsCarInfo.Car.EditWin({
                        items: [carEditForm]
                    });
                    carEditWin.show();
                }
            }, "-", {
                xtype: "tbardelbtn",
                handler: function (btn, e) {
                    JsHelper.DelConfirm(function (btn) {
                        if (btn == 'yes') {
                            alert(btn);
                            var s = cfg.tbargrid.getSelectionModel().getSelections();
                            for (var i = 0, r; r = s[i]; i++) {
                                cfg.tbargrid.getStore().remove(r);
                            }
                        }
                    });
                }
            }]
        }, cfg));
    }
});

//Car Default GridColumnMode
FsCarInfo.Car.DefaultColumnModel = Ext.extend(Ext.grid.ColumnModel, {
    sm: null,
    constructor: function (cfg) {
        cfg = cfg || {};
        FsCarInfo.Car.DefaultColumnModel.superclass.constructor.call(this, Ext.apply({
            columns: [
                new Ext.grid.RowNumberer(),
                new Ext.grid.CheckboxSelectionModel(),
                { header: "总仓", dataIndex: 'TDeport' },
                { header: "展场", dataIndex: 'CurrentDep' },
                { header: "品牌", dataIndex: 'Brand' },
                { header: "型号", dataIndex: 'Model' }
            ]
        }, cfg));
    }
});

//Car DataGrid
FsCarInfo.Car.GridPanel = Ext.extend(Ext.grid.GridPanel, {
    constructor: function (cfg) {
        cfg = cfg || {};
        FsCarInfo.Car.GridPanel.superclass.constructor.call(this, Ext.apply({
            stripeRows: true,
            frame: true,
            border: true,
            layout: "fit",
            loadMask: true,
            enableColumnMove: false,
            store: new FsCarInfo.Car.JsonStore(),
            sm: new Ext.grid.CheckboxSelectionModel(),
            cm: new FsCarInfo.Car.DefaultColumnModel(),
            tbar: new FsCarInfo.Car.DefaultToolbar({
                tbargrid: this
            }),
            bbar: new Infrastructure.BasePagingToolbar({
                store: cfg.store,
                plugins: cfg.plugins
            })
        }, cfg));
    }
});

//Car EditForm
FsCarInfo.Car.EditForm = Ext.extend(Infrastructure.BaseForm, {
    constructor: function (cfg) {
        cfg = cfg || {};

        //Form第一列
        var firstColumn = [{
            xtype: 'dictcombo',
            category: 'TDeport',
            name: 'TDeport',
            fieldLabel: '总仓'
        }, {
            xtype: 'dictcombo',
            category: 'Brand',
            name: 'Brand',
            fieldLabel: '品牌'
        }, {
            xtype: 'dictcombo',
            category: 'Color',
            name: 'Color',
            fieldLabel: '颜色'
        }, {
            xtype: 'dictcombo',
            category: 'Model',
            fieldLabel: '型号'
        }, {
            xtype: 'deptcombo',
            name: 'CurrentDep',
            fieldLabel: '展场'
        }, {
            fieldLabel: '生产日期',
            xtype: 'datefield',
            name: 'Prod_Date',
            width: 150,
            format: 'Y年m月d日'
        }];

        //Form第二列
        var secondColumn = [{
            fieldLabel: '发动机号<span style="color: red;" >*</span>',
            width: 150,
            name: 'Engin_ID',
            allowBlank: false,
            rvOptions: {
                url: '/Car/IsFieldExist',
                params: {
                    'Id': "-1",
                    'FieldName': 'Engin_ID'
                }
            },
            plugins: [Ext.ux.plugins.RemoteValidator]
        }, {
            fieldLabel: '车架号<span style="color: red;" >*</span>',
            width: 150,
            name: 'Frame_ID',
            allowBlank: false,
            rvOptions: {
                url: '/Car/IsFieldExist',
                params: {
                    'Id': "-1",
                    'FieldName': 'Frame_ID'
                }
            },
            plugins: [Ext.ux.plugins.RemoteValidator]
        }, {
            fieldLabel: '公里数',
            xtype: 'numberfield',
            width: 150,
            name: 'Miles'
        }, {
            xtype: 'cnMoneyfield',
            fieldLabel: '估价',
            name: 'Appraisal'
        }, {
            xtype: 'cnMoneyfield',
            fieldLabel: '实际成本',
            name: 'Cost'
        }, {
            fieldLabel: '增值税号',
            width: 150,
            name: 'VatID'
        }];

        //Form单列
        var singleColumn = [{
            autoHeight: true,
            layout: 'column',
            columns: 3,
            xtype: 'checkboxgroup',
            id: 'atmGroup',
            anchor: '100%',
            fieldLabel: '附件',
            //itemCls: 'x-check-group-alt',
            items: [{
                boxLabel: '全选',
                name: 'Atm_All',
                inputValue: '1',
                listeners: {
                    'check': function () {
                        var group = Ext.getCmp('atmGroup');
                        var length = group.items.getCount();
                        var all;
                        if (this.checked == true) {
                            all = true;
                        } else {
                            all = false;
                        }
                        for (i = 0; i < length; i++) {
                            var colItems = group.items.get(i);
                            var jlength = colItems.items.getCount();
                            for (j = 0; j < jlength; j++) {
                                colItems.items.get(j).setValue(all);
                            }
                        }
                    }
                }
            }, {
                boxLabel: '工具包', name: 'Atm_GJB', inputValue: '1'
            }, {
                boxLabel: '车钥匙', name: 'Atm_KEY', inputValue: '1'
            }, {
                boxLabel: '备胎', name: 'Atm_BT', inputValue: '1'
            }, {
                boxLabel: '点烟器', name: 'Atm_DYQ', inputValue: '1'
            }, {
                boxLabel: '雨刮', name: 'Atm_WIPER', inputValue: '1'
            }, {
                boxLabel: '天线', name: 'Atm_MAST', inputValue: '1'
            }, {
                boxLabel: '合格证', name: 'Atm_CERTIFICATE', inputValue: '1'
            }, {
                boxLabel: '说明书', name: 'Atm_SMS', inputValue: '1'
            }, {
                boxLabel: '保修卡', name: 'Atm_BXK', inputValue: '1'
            }]
        }, {
            xtype: 'textarea',
            fieldLabel: '描述',
            name: 'Remark',
            height: 50,
            width: 430
        }];

        FsCarInfo.Car.EditForm.superclass.constructor.call(this, Ext.apply({
            items: [{
                layout: 'column',
                // 注意 现在使用的是两列 要是三列或更多
                // 请改变下面的items
                // 并在上面定义每个列的组件集合
                items: [{
                    columnWidth: .5,
                    bodyStyle: 'padding:1px',
                    layout: 'form',
                    defaultType: 'textfield',
                    items: firstColumn
                }, {
                    columnWidth: .5,
                    bodyStyle: 'padding:1px',
                    layout: 'form',
                    defaultType: 'textfield',
                    items: secondColumn
                }]
            }, singleColumn],
            reader: new FsCarInfo.Car.JsonReader()
        }, cfg));
    }
});

//Car DetailWin
FsCarInfo.Car.EditWin = Ext.extend(Ext.Window, {
    constructor: function (cfg) {
        cfg = cfg || {};
        FsCarInfo.Car.EditWin.superclass.constructor.call(this, Ext.apply({
            title: '商品车信息',
            iconCls: 'application_form',
            width: 600,
            resizable: false,
            constrain: true,
            autoHeight: true,
            modal: true,
            closeAction: 'close',
            plain: true,
            items: [new FsCarInfo.Car.EditForm()],
            buttonAlign: 'center'
        }, cfg));
    }
});

